<?php
	namespace app\tactics\admin;
	
	use app\admin\controller\Admin;
	use app\common\builder\ZBuilder;
	use think\Db;
	use app\tactics\model\Membergroup as GroupModel;
	use app\tactics\model\Grouplist as Grouplist;
	use app\tactics\model\Member as MemberModel;
	use app\tactics\model\Memberlevel;

	/**
	 * 客户分组控制器
	 * @package app\tactics\controller
	 */
	class Membergroup extends Admin{
		
		public function index(){
			
			$map = $this->getMap();
			
			$data = GroupModel::where($map)->paginate();

			//添加右侧操作按钮
			$btn_view = [
				'title' => '查看',
				'icon'  => 'fa fa-fw fa-eye',
				'href'  => url('groupDetail', ['id' => '__id__'])
			];
			
			return 	ZBuilder::make('table')
					->addColumns([
						['name','分组名称'],
						['id','包含客户','callback',function($id){
							return Grouplist::where('groupid',$id)->count()." 个";
						}],
						['remark','分组备注'],
						['createtime','创建日期'],
						['updatetime','更新日期'],
						['right_button', '操作', 'btn']
					])
					->setTableName('member') // 指定数据表名
					->addTopButtons('add,delete') // 批量添加顶部按钮
					->addRightButton('edit',$btn_view)
					->setRowList($data)
					->fetch();
		}
		
		//向分组中添加客户
		public function addUserToGroup($GroupID = ''){
			if($this->request->isPost()){
				$data = $this->request->post();
				$ids = explode(',',$data['ids']);
				$GroupID = $data['Group'];
				for($i = 0;$i < sizeof($ids);$i++){
					$map = [];
					$map['memberid'] = $ids[$i];
					$map['groupid'] = $GroupID;
					$num = Db::name('member_grouplist')->where($map)->find();
					if(!empty($num)){
						Db::name('member_grouplist')->where('id',$num['id'])->update($map);
						$this->success('新增成功',url('/tactics/membergroup/groupDetail/id/'.$GroupID));
					}else{
						Db::name('member_grouplist')->insert($map);
						$this->success('新增成功',url('/tactics/membergroup/groupDetail/id/'.$GroupID));
					}
				}
				
			}else{
				$data = $this->request->get();
				$data = IdsToStr($data['ids']);
				$Group = Db::name('member_group')->select();
				$Group = ts_key($Group,'id','name');
				return	ZBuilder::make('form')
						->addSelect('Group','添加到','将所选客户添加到一个现有分组',$Group)
						->addHidden('ids',$data)
						->fetch();		
			}
			
		}
		//选中客户创建组
		public function createGroup($GroupID = ''){
						
			if($this->request->isPost()){
				$data = $this->request->post();
				$ids = explode(',',$data['ids']);
				$item = [];
				$item['name'] = $data['GroupName'];
				$item['remark'] = $data['GroupRemark'];
				GroupModel::insert($item);
				$GroupID = GroupModel::getLastInsID();
				for($i = 0;$i < sizeof($ids);$i++){
					$map = [];
					$map['memberid'] = $ids[$i];
					$map['groupid'] = $GroupID;
					$num = Db::name('member_grouplist')->where($map)->find();
					if(!empty($num)){
						Db::name('member_grouplist')->where('id',$num['id'])->update($map);
						$this->success('新增成功',url('/tactics/membergroup/groupDetail/id/'.$GroupID));
					}else{
						Db::name('member_grouplist')->insert($map);
						$this->success('新增成功',url('/tactics/membergroup/groupDetail/id/'.$GroupID));
					}
				}
				
			}else{
				$data = $this->request->get();
				$data = IdsToStr($data['ids']);
				$Group = Db::name('member_group')->select();
				$Group = ts_key($Group,'id','name');
				return	ZBuilder::make('form')
						->addText('GroupName','添加到','输入分组名称')
						->addText('GroupRemark','备注')
						->addHidden('ids',$data)
						->layout(['GroupName'=>6,'GroupRemark'=>6])
						->fetch();		
			}
		}
		
		//选择客户
		public function add($record = []){
			
			$map = [];
			
			return 	ZBuilder::make('mix')
					->addText('MixField', '条件','客户名称，手机号，联系人模糊搜索')
					->layout(['MixField'=>2])
					->setBtnTitle('submit', '查询')
					->addColumns([
						['company','客户名称'],
						['name','联系人'],
						['mobile','联系电话'],
						['province','省份'],
						['city','城市'],
						['town','区县'],
						['addr','详细地址'],
						['status','状态','status',['正常','禁用']],
					])
					->addTopButton('custom', ['title'=>'生成组','icon'=>'fa fa-fw fa-group','href'  => url('createGroup'),'class' => 'btn btn-primary js-get'])
					->addTopButton('custom', ['title'=>'添加到组','icon'=>'fa fa-fw fa-group','href'  => url('addUserToGroup'),'class' => 'btn btn-primary js-get'])
					->setRowList(Db::table('xb_member')->where($map)->select())
					->fetch();

		}

		
		public function groupDetail(){
			$GroupID = input('id');
			$Group = GroupModel::where('id',$GroupID)->find();
			$items = $Group ->listItem()->select();	
			$list = [];
			for($i=0;$i<sizeof($items);$i++){
				$GroupItem = Grouplist::get($items[$i]->id);
				$Member = MemberModel::get($GroupItem->memberid);
				$list[$i]['id'] = $GroupItem->id;
				$list[$i]['name'] = $Member->name;
				$list[$i]['company'] = $Member->company;
				$list[$i]['mobile'] = $Member->mobile;
				$list[$i]['addr'] = $Member->addr;
			}
			return	ZBuilder::make('mix')
					->addStatic('name','分组名称')
					->addStatic('remark','备注')
					->addStatic('createtime','创建时间')
					->addStatic('updatetime','创建时间')
					->setFormData($Group)
					->setTableName('member_grouplist') // 指定数据表名
					->layout(['name'=>3,'remark'=>3,'createtime'=>3,'updatetime'=>3])
					->hidebtn(['submit'])
					->addColumns([
						['company','公司名称'],
						['name','联系人'],
						['mobile','联系电话'],
						['addr','地址'],
						['CreateTime','加入时间'],
						['right_button', '操作', 'btn']
					])
					->hideCheckBox()
					->setPrimaryKey('id')
					->addRightButton('delete',['title'=>'移出该组'])
					->setRowList($list)
					->fetch();
		}
		
	}

?>
